//
//  JSTIMListViewController.m
//  KYKJPackDemo
//
//  Created by 01 on 17/6/21.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTIMListViewController.h"
#import "KYLoginController.h"
#import "KYNavigationController.h"
#import "SPKitExample.h"
#import "MJRefresh.h"
@interface JSTIMListViewController ()
@property (nonatomic, strong) YWConversationListViewController *listViewController;

@property (nonatomic, strong) UIView *noDataView;

@property (nonatomic, strong) UIView *needLoginView;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation JSTIMListViewController

- (void)dealloc
{
    [_timer invalidate];
    _timer = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(checkStatus) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //检查当前状态
    [self checkStatus];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!kAppDelegate.isTaobaoIMLogin) {
            [self openLoginVC];
        }
    });
}
- (void)viewWillLayoutSubviews
{
    self.view.frame = kAppDelegate.window.bounds;
}

- (void)changeConversationListViewController
{
    // 会话列表页面
    _listViewController = [[SPKitExample sharedInstance] exampleMakeConversationListControllerWithSelectItemBlock:^(YWConversation *aConversation){
        [self.navigationController pushViewController:[[SPKitExample sharedInstance] exampleMakeConversationViewControllerWithConversation:aConversation] animated:YES];}];
    _listViewController.view.frame = CGRectMake(0, kNavBarHeight, kDeviceWidth, kDeviceHeight - kNavBarHeight - kTabbarHeight);
    [_listViewController.tableView addHeaderWithTarget:self action:@selector(checkStatus)];
    [self.view addSubview:self.listViewController.view];
}


/**
 检查当前状态
 */
- (void)checkStatus{
    [_listViewController.tableView headerEndRefreshing];
    //没有登录淘宝服务器，提示登录
   if (!kAppDelegate.isTaobaoIMLogin){
       [self.view addSubview:self.needLoginView];
   }else{
       [self.needLoginView removeFromSuperview];
       static dispatch_once_t onceToken;
       dispatch_once(&onceToken, ^{
           [self changeConversationListViewController];
       });
       if (_listViewController.ywFRC.countOfFetchedObjects == 0){
           [self.view addSubview:self.noDataView];
       }else{
           [self.noDataView removeFromSuperview];
       }
   }
}

- (void) createNoDataView
{
    CGRect frame = CGRectMake(0, 0, 100, 100);
    UIView *viewForNoData = [[UIView alloc] initWithFrame:frame];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"general_btn_normal"]];
    imageView.center = CGPointMake(viewForNoData.frame.size.width/2, viewForNoData.frame.size.height/2);
    [imageView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin];
    [viewForNoData addSubview:imageView];
}

- (void)openLoginVC{
    KYLoginController *loginVc = [[KYLoginController alloc] init];
    KYNavigationController *navVC = [[KYNavigationController alloc] initWithRootViewController:loginVc];
    [self presentViewController:navVC animated:YES completion:nil];
}
- (UIView *)noDataView
{
    if (!_noDataView) {
        _noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, kCurrentScreenWidth(200), kDeviceWidth, kCurrentScreenWidth(50))];
        UILabel *label = [[UILabel alloc] initWithFrame:_noDataView.bounds];
        label.text = @"暂无消息";
        label.textAlignment = NSTextAlignmentCenter;
        [_noDataView addSubview:label];
    }
    return _noDataView;
}
-(UIView *)needLoginView{
    if (!_needLoginView) {
        _needLoginView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        UIButton *loginBtn = [[UIButton alloc] init];
        [loginBtn setTitle:@"点击登录后查看" forState:UIControlStateNormal];
        [loginBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        loginBtn.layer.borderWidth = 0.5;
        loginBtn.layer.cornerRadius = 2;
        loginBtn.layer.borderColor = [UIColor grayColor].CGColor;
        [loginBtn sizeToFit];
        [_needLoginView addSubview:loginBtn];
        loginBtn.center = _needLoginView.center;
        [loginBtn addTarget:self action:@selector(openLoginVC) forControlEvents:UIControlEventTouchUpInside];
    }
    return _needLoginView;
}
- (YWConversationListViewController *)listViewController
{
    if (!_listViewController) {
        _listViewController = [[SPKitExample sharedInstance] exampleMakeConversationListControllerWithSelectItemBlock:nil];
    }
    return _listViewController;
}

@end
